package punchdownloader.threads;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import punchdownloader.Modelos.Anime;

/**
 * 
 * @author Kennedy
 * 
 */
public class DownloadImagens extends Thread {

    private String url_projetos;
    private ArrayList<Anime> lista_anime_compartilhada;
    private Logger log = Logger.getLogger("Thread - Download Thumbs");
    
    public DownloadImagens(String url_projetos, ArrayList<Anime> lista_animes) {
        this.url_projetos = url_projetos;
        this.lista_anime_compartilhada = lista_animes;
    }

    public void download() {
        File aux;
        String url_projects;

        for (int j = 1; j < 4; j++) {
            url_projects = url_projetos + j;

            try {
                Parser p = new Parser(url_projects);
                NodeList img_links = p.extractAllNodesThatMatch(new TagNameFilter("img"));
                for (int i = 0; i < img_links.size(); i++) {
                    if (img_links.elementAt(i).getText().contains("imagens/projetos/animes")) {
                        ImageTag img = (ImageTag) img_links.elementAt(i);

                        try {
                            URL url = new URL(img.getImageURL());
                            File dir = new File(new File("").getAbsolutePath() + "/img_cache");
                            dir.mkdir();

                            aux = new File(dir.getAbsolutePath() + "/" + url.getFile().substring(url.getFile().lastIndexOf("/") + 1));
                            if (aux.exists()) {
                                continue;
                            }
                            InputStream input = url.openStream();
                            FileOutputStream fo = new FileOutputStream(new File(dir.getAbsolutePath() + "/" + url.getFile().substring(url.getFile().lastIndexOf("/") + 1)));

                            int Byte = 0;

                            while ((Byte = input.read()) != -1) {
                                fo.write(Byte);
                            }

                            input.close();
                            fo.close();

                        } catch (IOException iOException) {
                            //Erro ao fazer download
                        }

                    }
                }
            } catch (ParserException ex) {
                
            }

        }

    }

    private void carregaThumbs() {

        File tmp = new File(new File("").getAbsolutePath() + "/img_cache"); //Pega o diretorio atual
        String a[] = tmp.list();

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < lista_anime_compartilhada.size(); j++) {
                if (a[i].contains(".jpg") && a[i].contains("_thumb2")) {
                    if (lista_anime_compartilhada.get(j).getId().equalsIgnoreCase(a[i].substring(0, a[i].indexOf("_")))) {
                        lista_anime_compartilhada.get(j).setThumb(new ImageIcon(tmp.getAbsolutePath() + "/" + a[i]));
                    }
                }
            }

        }
    }

    public void run() {
        download();
        carregaThumbs();
    }
}
